#!/usr/bin/env python
"""
调试特定简历内容提取
"""
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'inter_api.settings')
django.setup()

from django.contrib.auth import get_user_model
from students.models import Student
from resume_management.models import Resume, BasicInfo, Education, Skill, Project, WorkExperience, Award, SelfIntroduction

User = get_user_model()

def debug_specific_resume(resume_id):
    """调试特定简历内容"""
    print(f"调试简历ID={resume_id}的内容提取...")
    
    try:
        # 获取指定简历
        resume = Resume.objects.get(id=resume_id)
        
        print(f"找到简历: ID={resume.id}, 标题={resume.title}, 类型={resume.resume_type}")
        print(f"完成度: {resume.completion_rate}%")
        print(f"状态: {resume.status}")
        print(f"学生: {resume.student.name}")
        
        # 检查基本信息
        print("\n=== 基本信息 ===")
        try:
            basic_info = resume.basicinfo
            print(f"姓名: {basic_info.name}")
            print(f"性别: {basic_info.gender}")
            print(f"电话: {basic_info.phone}")
            print(f"邮箱: {basic_info.email}")
            print(f"地址: {basic_info.address}")
        except BasicInfo.DoesNotExist:
            print("❌ 没有基本信息")
        except Exception as e:
            print(f"❌ 基本信息错误: {e}")
        
        # 检查教育经历
        print("\n=== 教育经历 ===")
        education_list = resume.education_set.all()
        print(f"教育经历数量: {education_list.count()}")
        for edu in education_list:
            print(f"- {edu.school_name} | {edu.major} | {edu.degree}")
        
        # 检查技能
        print("\n=== 技能特长 ===")
        skill_list = resume.skill_set.all()
        print(f"技能数量: {skill_list.count()}")
        for skill in skill_list:
            print(f"- {skill.name} | {skill.level}")
        
        # 检查项目经历
        print("\n=== 项目经历 ===")
        project_list = resume.project_set.all()
        print(f"项目数量: {project_list.count()}")
        for project in project_list:
            print(f"- {project.name} | {project.role}")
        
        # 检查工作经历
        print("\n=== 工作经历 ===")
        work_list = resume.workexperience_set.all()
        print(f"工作经历数量: {work_list.count()}")
        for work in work_list:
            print(f"- {work.company_name} | {work.position}")
        
        # 检查获奖情况
        print("\n=== 获奖情况 ===")
        award_list = resume.award_set.all()
        print(f"获奖数量: {award_list.count()}")
        for award in award_list:
            print(f"- {award.award_name or award.name} | {award.date_received}")
        
        # 检查自我介绍
        print("\n=== 自我介绍 ===")
        try:
            intro = resume.selfintroduction
            print(f"自我介绍: {intro.content[:100] if intro.content else '空'}...")
        except SelfIntroduction.DoesNotExist:
            print("❌ 没有自我介绍")
        except Exception as e:
            print(f"❌ 自我介绍错误: {e}")
        
        # 测试内容提取
        print("\n=== 内容提取测试 ===")
        from resume_management.views import ResumeViewSet
        viewset = ResumeViewSet()
        content = viewset._extract_resume_content(resume)
        
        print(f"提取的内容长度: {len(content)}")
        if content:
            print("提取的内容预览:")
            print(content[:500] + "..." if len(content) > 500 else content)
        else:
            print("❌ 提取的内容为空")
            
        # 检查上传文件
        print("\n=== 上传文件 ===")
        if resume.uploaded_file:
            print(f"上传文件: {resume.uploaded_file}")
        else:
            print("❌ 没有上传文件")
            
        # 检查生成文件
        print("\n=== 生成文件 ===")
        if resume.generated_file:
            print(f"生成文件: {resume.generated_file}")
        elif resume.generated_file_url:
            print(f"生成文件URL: {resume.generated_file_url}")
        else:
            print("❌ 没有生成文件")
            
    except Resume.DoesNotExist:
        print(f"❌ 简历ID={resume_id}不存在")
    except Exception as e:
        print(f"调试失败: {e}")
        import traceback
        traceback.print_exc()

def reset_resume_ai_status(resume_id):
    """重置指定简历的AI评估状态"""
    try:
        resume = Resume.objects.get(id=resume_id)
        print(f"重置简历ID={resume_id}的AI评估状态...")
        print(f"当前状态: {resume.ai_evaluation_status}")

        resume.ai_evaluation_status = 'not_started'
        resume.ai_evaluation_error = None
        resume.ai_evaluation_file = None
        resume.ai_evaluation_file_url = None
        resume.ai_evaluation_job_position = None
        resume.ai_evaluation_at = None
        resume.save()

        print(f"✅ 已重置简历AI评估状态为: {resume.ai_evaluation_status}")

    except Resume.DoesNotExist:
        print(f"❌ 简历ID={resume_id}不存在")
    except Exception as e:
        print(f"重置失败: {e}")

if __name__ == '__main__':
    # 调试简历ID=27
    debug_specific_resume(27)

    # 重置AI评估状态
    reset_resume_ai_status(27)
